package com.rain.shoppingscienceproj.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.rain.shoppingscienceproj.ui.utile.PowerUtils;
import com.rain.shoppingscienceproj.utils.AppManager;
import com.rain.shoppingscienceproj.utils.rxBus.RxBus;

import cn.jpush.android.api.JPushInterface;

/**
 * Author: Rain
 * Create: 2018/1/10 9:40
 */

public class MessageReceiver extends BroadcastReceiver {
    private static final String TAG = "MyReceiver";
    public static Handler mHandler;
    private static final String activityNameMessage = "com.rain.shoppingscienceproj.ui.activity.message.MessageActivity";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "JPush用户注册成功");
        Bundle bundle = intent.getExtras();
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            Log.d(TAG, "JPush用户注册成功");

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "接受到推送下来的自定义消息");

        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "接受到推送下来的通知");
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            if (PowerUtils.isRun(context)) {
                String activityName = AppManager.getAppManager().getTopActivityName();
                if (activityName.equals(activityNameMessage)) {
                    RxBus.getInstance().post(new String("MessageService"));
                } else {
                    RxBus.getInstance().post(new String("MessageServiceIntent"));
                }
            } else {
                Log.i("NotificationReceiver", "the app process is dead");
                Intent launchIntent = context.getPackageManager().
                        getLaunchIntentForPackage(PowerUtils.getAppInfo(context));
                launchIntent.setFlags(
                        Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                context.startActivity(launchIntent);
            }
        } else {
            Log.d(TAG, "Unhandled intent - " + intent.getAction());
        }
    }
}
